package com.lg.partition22;

import java.io.*;

/**
 * @author `RKC`
 * @date 2022/2/28 22:00
 */
public class P2196挖地雷 {

    private static final int N = 25;
    private static boolean[][] g = new boolean[N][N];
    private static int[] f = new int[N], rec = new int[N], w = new int[N];
    private static int n = 0;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = Integer.parseInt(reader.readLine());
        String[] ss = reader.readLine().split(" ");
        for (int i = 1; i <= n; i++) {
            w[i] = Integer.parseInt(ss[i - 1]);
        }
        for (int i = 1; i < n; i++) {
            ss = reader.readLine().split(" ");
            for (int j = 0; j < ss.length; j++) {
                if (Integer.parseInt(ss[j]) == 1) {
                    g[i][i + j + 1] = true;
                }
            }
        }
        int ans = 0, start = 0;
        for (int i = 1; i <= n; i++) {
            int res = dfs(i);
            if (ans < res) {
                ans = res;
                start = i;
            }
        }
        while (start != 0) {
            writer.write(start + " ");
            start = rec[start];
        }
        writer.write("\n" + ans + "\n");
        writer.flush();
    }

    private static int dfs(int u) {
        if (f[u] != 0) return f[u];
        f[u] += w[u];
        for (int i = 1; i <= n; i++) {
            if (g[u][i]) {
                int res = dfs(i) + w[u];
                if (res > f[u]) {
                    f[u] = res;
                    rec[u] = i;
                }
            }
        }
        return f[u];
    }
}
